package com.jz.community.moduleshopping.shopCart.model;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.baseGoods.GoodInfo;
import com.jz.community.basecomm.bean.baseGoods.ShopInfo;
import com.jz.community.basecomm.bean.baseGoods.ShopListInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.intentUtils.OrderIntentUtils;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.shopCart.bean.CartGoodInfo;
import com.jz.community.moduleshopping.shopCart.bean.CartPostageInfo;
import com.jz.community.moduleshopping.shopCart.bean.CartShopInfo;
import com.jz.community.moduleshopping.shopCart.bean.NewCartListInfo;
import com.jz.community.moduleshopping.shopCart.net.ShopCartApi;
import com.jz.community.moduleshopping.shopCart.ui.ShopCartBalanceDialog;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Tony.Fan on 2018/5/18 14:01
 * <p>
 * 购物车基础业务逻辑
 */
public class ShopCartModel {

    private NewCartListInfo newCartListInfo;

    private NewCartListInfo newRefreshCartListInfo;

    //购物车一些状态判断
    private ShoppingCartStatus shoppingCartStatus;

    public static final String CHECK = "Y";
    public static final String UNCHECK = "N";

    private static final String GOODS_CART_TYPE = "1"; //电商
    private static final String SHOP_CART_TYPE = "2"; //楼下小店

    private ShopCartBalanceDialog shopCartBalanceDialog;

    private Context mContext;

    public ShopCartModel(Context ctx) {
        mContext = ctx;
        shoppingCartStatus = new ShoppingCartStatus();
        shopCartBalanceDialog = new ShopCartBalanceDialog(ctx);
        toConfirmOrderCallBack();
    }

    public void setNewCartListInfo(NewCartListInfo newCartListInfo) {
        this.newCartListInfo = newCartListInfo;
    }

    public void setNewRefreshCartListInfo(NewCartListInfo newCartListInfo) {
        this.newRefreshCartListInfo = newCartListInfo;
    }

    private CartPostageInfo cartPostageInfo;


    public void setCartPostageInfo(CartPostageInfo cartPostageInfo) {
        this.cartPostageInfo = cartPostageInfo;
    }

    /**
     * 参与邮费活动店铺id
     *
     * @return
     */
    private String[] cartShopIds() {
        if (!Preconditions.isNullOrEmpty(cartPostageInfo)) {
            return cartPostageInfo.getShopId().split(",");
        }
        return null;
    }


    public List<CartShopInfo> getShopInfoList() {

        if (!Preconditions.isNullOrEmpty(newCartListInfo) && !Preconditions.isNullOrEmpty(newCartListInfo.getValidGoods())) {
            return newCartListInfo.getValidGoods();
        }
        return null;
    }

    public List<CartGoodInfo> getCartInfoList(CartShopInfo shopInfosBean) {

        if (!Preconditions.isNullOrEmpty(shopInfosBean) && !Preconditions.isNullOrEmpty(shopInfosBean.getCartInfos())) {
            return shopInfosBean.getCartInfos();
        }
        return null;
    }

    /**
     * 刷新数据选中状态处理
     *
     * @return
     */
    public List<CartShopInfo> getRefreshCartListInfo() {
        List<CartShopInfo> cartShopInfoList = getShopInfoList();
        List<CartShopInfo> cartRefreshShopInfos = newRefreshCartListInfo.getValidGoods();
        for (CartShopInfo cartShopInfo : cartShopInfoList) {
            for (CartShopInfo cartRefreshShopInfo : cartRefreshShopInfos) {
                if (cartShopInfo.getId().equals(cartRefreshShopInfo.getId())) {
                    if (cartShopInfo.getChecked().equals(CHECK)) {
                        cartRefreshShopInfo.setChecked(CHECK);
                        getCartRefreshGoodInfos(null, cartRefreshShopInfo.getCartInfos(), false);
                    } else {
                        getCartRefreshShopInfos(cartShopInfo, cartRefreshShopInfo);
                    }
                }
            }
        }
        newCartListInfo.setValidGoods(cartRefreshShopInfos);
        return cartRefreshShopInfos;
    }

    /**
     * 刷新店铺选中状态
     *
     * @param cartShopInfo
     * @param cartRefreshShopInfo
     */
    private void getCartRefreshShopInfos(CartShopInfo cartShopInfo, CartShopInfo cartRefreshShopInfo) {
        List<CartGoodInfo> cartGoodInfos = cartShopInfo.getCartInfos();
        List<CartGoodInfo> cartRefreshGoodInfos = cartRefreshShopInfo.getCartInfos();
        for (CartGoodInfo cartGoodInfo : cartGoodInfos) {
            getCartRefreshGoodInfos(cartGoodInfo, cartRefreshGoodInfos, true);
        }
    }

    /**
     * 刷新商品选中状态
     *
     * @param cartGoodInfo
     * @param cartRefreshGoodInfos
     * @param isCheck
     */
    private void getCartRefreshGoodInfos(CartGoodInfo cartGoodInfo, List<CartGoodInfo> cartRefreshGoodInfos, boolean isCheck) {
        for (CartGoodInfo cartRefreshGoodInfo : cartRefreshGoodInfos) {
            if (isCheck) {
                if (cartGoodInfo.getId().equals(cartRefreshGoodInfo.getId())) {
                    if (isCheck(cartRefreshGoodInfo)) {
                        cartRefreshGoodInfo.setChecked(CHECK);
                    } else {
                        cartRefreshGoodInfo.setChecked(UNCHECK);
                    }
                }
            } else {
                cartRefreshGoodInfo.setChecked(CHECK);
            }
        }
    }

    /**
     * 更新结算选中的商品
     */
    public void changeBalanceSelectedGoods() {
        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return;
        }
        List<CartGoodInfo> cartInfoList = new ArrayList<>();
        for (CartShopInfo shoppingItem : getShopInfoList()) {
            addCartGoodInfo(shoppingItem, cartInfoList);
        }
        showBalanceGoodsInfo(cartInfoList);
    }

    /**
     * 购物商品对象
     *
     * @param shoppingItem
     * @param cartInfoList
     */
    private void addCartGoodInfo(CartShopInfo shoppingItem, List<CartGoodInfo> cartInfoList) {
        for (int i = 0; i < shoppingItem.getCartInfos().size(); i++) {

            if (isCheck(shoppingItem.getCartInfos().get(i))) {

                if (GOODS_CART_TYPE.equals(shoppingItem.getCartInfos().get(i).getCartType())) {

                    //迭代删除相同数据
                    if (!Preconditions.isNullOrEmpty(cartInfoList)) {
                        Iterator<CartGoodInfo> it = cartInfoList.iterator();
                        while (it.hasNext()) {
                            if (it.next().getCartType().equals(GOODS_CART_TYPE)) {
                                it.remove();
                            }
                        }
                    }
                    addCartInfoList(shoppingItem, cartInfoList, i, mContext.getString(R.string.cart_goods_name));
                } else {
                    if (shoppingItem.getId().equals(shoppingItem.getCartInfos().get(i).getShopId())) {
                        addCartInfoList(shoppingItem, cartInfoList, i, shoppingItem.getName());
                        return;
                    } else {
                        addCartInfoList(shoppingItem, cartInfoList, i, shoppingItem.getName());
                    }
                }
            }
        }
    }

    /**
     * 不同购物车类型添加集合
     *
     * @param shoppingItem
     * @param cartInfoList
     * @param index
     * @param goodsName
     */
    private void addCartInfoList(CartShopInfo shoppingItem, List<CartGoodInfo> cartInfoList, int index, String goodsName) {
        cartInfoList.add(encapsulationBalanceGoodsInfo(shoppingItem.getCartInfos().get(index), shoppingItem, goodsName));
    }

    /**
     * 不同购物车类型对象
     *
     * @param shoppingItem
     * @return
     */
    private CartGoodInfo encapsulationBalanceGoodsInfo(CartGoodInfo cartInfosBean, CartShopInfo shoppingItem, String goodName) {
        CartGoodInfo itemInfo = new CartGoodInfo();
        if (GOODS_CART_TYPE.equals(cartInfosBean.getCartType())) {
            itemInfo.setGoodsName(goodName);
            itemInfo.setAddCartInfo(true);
            itemInfo.setPrice(ConverterUtils.toString(calculateTotalSelectedCartTypePrice(GOODS_CART_TYPE, shoppingItem.getId(), true)));
            itemInfo.setNum(calculateTotalSelectedCartTypeNum(GOODS_CART_TYPE, shoppingItem.getId(), true));
        } else {
            itemInfo.setGoodsName(shoppingItem.getName());
            itemInfo.setAddCartInfo(false);
            itemInfo.setPrice(ConverterUtils.toString(calculateTotalSelectedCartTypePrice(SHOP_CART_TYPE, shoppingItem.getId(), false)));
            itemInfo.setNum(calculateTotalSelectedCartTypeNum(SHOP_CART_TYPE, shoppingItem.getId(), false));
        }
        itemInfo.setInPos(cartInfosBean.getInPos());
        itemInfo.setCartType(cartInfosBean.getCartType());
        return itemInfo;
    }

    /**
     * 显示结算商品
     *
     * @param cartInfoList
     */
    private void showBalanceGoodsInfo(List<CartGoodInfo> cartInfoList) {
        if (!Preconditions.isNullOrEmpty(cartInfoList)) {
            if (cartInfoList.size() > 1) {
                shopCartBalanceDialog.addCartInfos(cartInfoList);
            } else {
                toConfirmOrderActivity(cartInfoList.get(0).getCartType(), cartInfoList.get(0).getInPos());
            }
        }
    }

    /**
     * 更新Item购买数量
     *
     * @param position
     * @param num
     */
    public void changeRecycleViewItemNum(int position, int num, int currentPos) {

        List<CartShopInfo> shoppingList = getShopInfoList();

        CartShopInfo shoppingItem = shoppingList.get(currentPos);

        List<CartGoodInfo> cartGoodInfos = shoppingItem.getCartInfos();

        cartGoodInfos.get(position).setNum(num);

        shoppingItem.setCartInfos(cartGoodInfos);

    }

    /**
     * 确定订单支付页
     *
     * @param cartType 结算类型
     */
    public void toConfirmOrderActivity(String cartType, int position) {
        String shopCartInfo = JSON.toJSONString(getShopCartInfoData(cartType, position));


        if (Preconditions.isNullOrEmpty(shopCartInfo)) {
            return;
        }

        if (GOODS_CART_TYPE.equals(cartType)) {
            OrderIntentUtils.startFromOrderActivity(shopCartInfo);
        } else {
            OrderIntentUtils.startFromOrderActivity(shopCartInfo);
        }
    }

    /**
     * 包装获取购物车数据
     *
     * @param cartType 1 电商 2 楼下小店
     * @param position
     * @return
     */
    private ShopInfo getShopCartInfoData(String cartType, int position) {
        List<ShopListInfo> shopLists = new ArrayList<>();
        List<CartShopInfo> shoppingList = getShopInfoList();
        ShopInfo shopInfo = new ShopInfo();
        if (SHOP_CART_TYPE.equals(cartType)) {
            CartShopInfo shoppingItem = shoppingList.get(position);
            if (CHECK.equals(shoppingItem.getChanged())) {
                ShopListInfo shopListInfo = shopListInfo(shoppingItem, shopInfo, false);
                List<GoodInfo> goodsList = new ArrayList<>();
                for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                    if (isCheck(cartGoodInfo)) {
                        goodsList.add(goodInfo(cartGoodInfo));
                    }
                }
                shopListInfo.setGoodsList(goodsList);
                shopLists.add(shopListInfo);
                shopInfo.setCartIds(cartIds(1, false, position));
                shopInfo.setShopList(shopLists);
            }
        } else {
            for (CartShopInfo shoppingItem : shoppingList) {
                if (GOODS_CART_TYPE.equals(shoppingItem.getCartType())) {
                    if (CHECK.equals(shoppingItem.getChanged())) {
                        ShopListInfo shopListInfo = shopListInfo(shoppingItem, shopInfo, true);
                        List<GoodInfo> goodsList = new ArrayList<>();
                        for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                            if (isCheck(cartGoodInfo)) {
                                goodsList.add(goodInfo(cartGoodInfo));
                            }
                        }
                        shopListInfo.setGoodsList(goodsList);
                        shopLists.add(shopListInfo);
                        shopInfo.setCartIds(cartIds(0, true, position));
                        shopInfo.setShopList(shopLists);
                    }
                }
            }
        }
        return shopInfo;
    }

    /**
     * 获取下单店铺数据
     *
     * @param shoppingItem
     */
    private ShopListInfo shopListInfo(CartShopInfo shoppingItem, ShopInfo shopInfo, boolean isCartType) {
        ShopListInfo shopListInfo = new ShopListInfo();
        shopListInfo.setShopId(shoppingItem.getId());
        shopListInfo.setShopName(shoppingItem.getName());
        shopInfo.setSendType(shoppingItem.getSend_type());
        shopInfo.setGoodFrom(isCartType == true ? 1 : 0);
        return shopListInfo;
    }


    /**
     * 商品基本信息
     *
     * @param cartGoodInfo
     * @return
     */
    private GoodInfo goodInfo(CartGoodInfo cartGoodInfo) {
        GoodInfo goods = new GoodInfo();
        goods.setId(cartGoodInfo.getGsId());
        goods.setNums(cartGoodInfo.getNum());
        goods.setImg(cartGoodInfo.getIcon());
        goods.setName(cartGoodInfo.getGoodsName());
        goods.setSkuName(cartGoodInfo.getSkuName());
        goods.setSkuId(cartGoodInfo.getSkuId());
        goods.setCategoryId(cartGoodInfo.getCategoryId());
        goods.setPrice(cartGoodInfo.getPrice());
        goods.setDisCountPrice(cartGoodInfo.getDiscountPrice());
        goods.setPickupPoint(cartGoodInfo.getPickupPoint());
        goods.setDistributionType(cartGoodInfo.getDistributionType());
        return goods;
    }

    /**
     * 设置 全选 与 否
     *
     * @param isAllSelect
     */
    public void setAllShopChk(boolean isAllSelect) {

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return;
        }

        for (int i = 0; i < getShopInfoList().size(); i++) {
            setAllGoodsChk(isAllSelect, i);
        }
    }

    /**
     * 设置所有商品选中与否
     *
     * @param currentPos
     */
    public void setAllGoodsChk(boolean isChk, int currentPos) {

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return;
        }

        CartShopInfo cartShopInfo = getShopInfoList().get(currentPos);
        cartShopInfo.setChecked(isChk ? CHECK : UNCHECK);
        cartShopInfo.setChanged(isChk ? CHECK : UNCHECK);
        List<CartGoodInfo> cartGoodInfos = cartShopInfo.getCartInfos();
        for (int i = 0; i < cartGoodInfos.size(); i++) {
            CartGoodInfo cartGoodInfo = getCartInfoList(cartShopInfo).get(i);
            cartGoodInfo.setChecked(isChk ? CHECK : UNCHECK);
            cartGoodInfo.setInPos(currentPos);
        }
    }

    /**
     * 设置某个商品选中与否
     *
     * @param itemPos
     * @param isChk
     * @param currentPos
     */
    public void setGoodsItemChk(int itemPos, boolean isChk, int currentPos, OnLoadListener<List<CartShopInfo>> onLoadListener) {

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return;
        }

        CartShopInfo cartShopInfo = getShopInfoList().get(currentPos);
        CartGoodInfo cartGoodInfo = getCartInfoList(cartShopInfo).get(itemPos);
        if (isChk) {
            cartGoodInfo.setChecked(isChk ? CHECK : UNCHECK);
            updateCurrentShopCheck(cartShopInfo);
        } else {
            cartGoodInfo.setChecked(isChk ? CHECK : UNCHECK);
            updateCurrentShopCheck(cartShopInfo);
        }
        cartGoodInfo.setInPos(currentPos);
        onLoadListener.onSuccess(getShopInfoList());

    }

    /**
     * 当前点击店铺否选中与否
     *
     * @param cartShopInfo
     * @return
     */
    private void updateCurrentShopCheck(CartShopInfo cartShopInfo) {
        List<CartGoodInfo> cartGoodInfos = getCartInfoList(cartShopInfo);
        if (CHECK.equals(getLastElement(cartGoodInfos).getChecked())) {
            if (isCheckShopItem(getLastElement(cartGoodInfos), cartShopInfo)) {
                cartShopInfo.setChecked(CHECK);
                cartShopInfo.setChanged(CHECK);
            } else {
                cartShopInfo.setChecked(UNCHECK);
                cartShopInfo.setChanged(CHECK);
            }
        } else {
            cartShopInfo.setChecked(UNCHECK);
            if (isCheckShopItem(getLastElement(cartGoodInfos), cartShopInfo)) {
                cartShopInfo.setChanged(UNCHECK);
            } else {
                cartShopInfo.setChanged(CHECK);
            }
        }
    }


    /**
     * 判断店铺是否选中
     *
     * @param cartGoodInfo
     * @param cartShopInfo
     * @return
     */
    private boolean isCheckShopItem(CartGoodInfo cartGoodInfo, CartShopInfo cartShopInfo) {
        for (CartGoodInfo cartGoodItem : cartShopInfo.getCartInfos()) {
            if (cartGoodInfo.getChecked().equals(cartGoodItem.getChecked())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 获取list中存放的最后一个元素
     * * @param list
     * * @param <T>
     * * @return
     */
    public static <T> T getLastElement(List<T> list) {
        return list.get(list.size() - 1);
    }

    /**
     * 设置某个店铺所有商品选中与否
     *
     * @param isChk
     * @param currentPos
     */
    public void setShopAllGoodsChk(boolean isChk, int currentPos, OnLoadListener<List<CartShopInfo>> onLoadListener) {

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return;
        }

        CartShopInfo cartShopInfo = getShopInfoList().get(currentPos);
        cartShopInfo.setChecked(isChk ? CHECK : UNCHECK);
        cartShopInfo.setChanged(isChk ? CHECK : UNCHECK);
        List<CartGoodInfo> cartGoodInfos = cartShopInfo.getCartInfos();
        for (int i = 0; i < cartGoodInfos.size(); i++) {
            CartGoodInfo cartGoodInfo = getCartInfoList(cartShopInfo).get(i);
            cartGoodInfo.setChecked(isChk ? CHECK : UNCHECK);
            cartGoodInfo.setInPos(currentPos);
        }
        onLoadListener.onSuccess(getShopInfoList());
    }

    /**
     * 判断是否全部选中结算
     *
     * @return
     */
    public boolean isAllSelectToPay() {

        return shoppingCartStatus.isAllSelectToEdit(getShopInfoList());
    }

    /**
     * 判断是否至少有一个选中
     *
     * @return
     */
    public boolean hasLeastOneChoose() {
        return shoppingCartStatus.hasLeastOneChoose(getShopInfoList());
    }

    /**
     * 获取购物id
     *
     * @return
     */
    private long[] cartIds(int cartUpdateState, boolean isCartType, int position) {
        List<CartShopInfo> shoppingList = getShopInfoList();
        long ids[] = new long[calculateSelectedGoodsNum(shoppingList, cartUpdateState, isCartType, position)];
        int nElements = 0;
        switch (cartUpdateState) {
            case 0:
                if (isCartType) {
                    for (CartShopInfo shoppingItem : shoppingList) {
                        if (shoppingItem.getCartType().equals(GOODS_CART_TYPE)) {
                            for (CartGoodInfo cartInfosBean : shoppingItem.getCartInfos()) {
                                if (isCheck(cartInfosBean)) {
                                    ids[nElements] = ConverterUtils.toLong(cartInfosBean.getId());
                                    nElements++;
                                }
                            }
                        }
                    }
                } else {
                    for (CartShopInfo shoppingItem : shoppingList) {
                        for (CartGoodInfo cartInfosBean : shoppingItem.getCartInfos()) {
                            if (isCheck(cartInfosBean)) {
                                ids[nElements] = ConverterUtils.toLong(cartInfosBean.getId());
                                nElements++;
                            }
                        }
                    }
                }
                break;
            case 1:
                CartShopInfo shoppingItem = shoppingList.get(position);
                if (shoppingItem.getCartType().equals(SHOP_CART_TYPE)) {
                    ids = getCardIds(shoppingItem, ids, nElements);
                }
                break;
        }
        return ids;
    }

    /**
     * 获取选中购物车id
     *
     * @param shoppingItem
     * @param ids
     * @param nElements
     * @return
     */
    private long[] getCardIds(CartShopInfo shoppingItem, long ids[], int nElements) {
        for (CartGoodInfo cartInfosBean : shoppingItem.getCartInfos()) {
            if (isCheck(cartInfosBean)) {
                ids[nElements] = ConverterUtils.toLong(cartInfosBean.getId());
                nElements++;
            }
        }
        return ids;
    }

    /**
     * 计算选择商品数量
     *
     * @param shoppingList
     * @return
     */
    private int calculateSelectedGoodsNum(List<CartShopInfo> shoppingList, int cartUpdateState, boolean isCartType, int position) {
        int calculateGoodsNum = 0;
        switch (cartUpdateState) {
            case 0:
                if (isCartType) {
                    for (CartShopInfo shoppingItem : shoppingList) {
                        if (shoppingItem.getCartType().equals(GOODS_CART_TYPE)) {
                            calculateGoodsNum = getCartIdsNum(shoppingItem, calculateGoodsNum);
                        }
                    }
                } else {
                    for (CartShopInfo shoppingItem : shoppingList) {
                        calculateGoodsNum = getCartIdsNum(shoppingItem, calculateGoodsNum);
                    }
                }
                break;
            case 1:
                CartShopInfo shoppingItem = shoppingList.get(position);
                if (shoppingItem.getCartType().equals(SHOP_CART_TYPE)) {
                    calculateGoodsNum = getCartIdsNum(shoppingItem, calculateGoodsNum);
                }
                break;
        }
        return calculateGoodsNum;
    }

    /**
     * 获取选中购物车id数量
     *
     * @param shoppingItem
     * @param calculateGoodsNum
     * @return
     */
    private int getCartIdsNum(CartShopInfo shoppingItem, int calculateGoodsNum) {
        for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
            if (isCheck(cartGoodInfo) && shoppingItem.getCartType().equals(GOODS_CART_TYPE)) {
                calculateGoodsNum++;
            } else if (isCheck(cartGoodInfo) && shoppingItem.getCartType().equals(SHOP_CART_TYPE)) {
                calculateGoodsNum++;
            }
        }
        return calculateGoodsNum;
    }

    /**
     * 计算选中的购物车数量
     *
     * @return
     */
    public int calculateTotalSelectedNum() {
        int totalSelectedQuantity = 0;
        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return 0;
        }
        List<CartShopInfo> shoppingList = getShopInfoList();
        for (CartShopInfo shoppingItem : shoppingList) {
            for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                if (isCheck(cartGoodInfo)) {
                    totalSelectedQuantity += cartGoodInfo.getNum();
                }
            }
        }
        return totalSelectedQuantity;
    }

    /**
     * 计算添加的购物车数量
     *
     * @return
     */
    public int calculateTotalAddNum() {
        int totalSelectedQuantity = 0;
        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return 0;
        }
        List<CartShopInfo> shoppingList = getShopInfoList();
        for (CartShopInfo shoppingItem : shoppingList) {
            for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                totalSelectedQuantity += cartGoodInfo.getNum();
            }
        }
        return totalSelectedQuantity;
    }

    /**
     * 计算选中的购物车数量邮费价格
     *
     * @return
     */
    public double calculateTotalSelectedPostagePrice() {

        double totalSelectedQuantity = 0;

        List<CartShopInfo> shoppingList = getShopInfoList();

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return totalSelectedQuantity;
        }

        for (CartShopInfo shoppingItem : shoppingList) {
            for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                if (isCheck(cartGoodInfo)) {
                    if (!Preconditions.isNullOrEmpty(cartShopIds())) {
                        for (String shopId : cartShopIds()) {
                            if (shopId.equals(cartGoodInfo.getShopId())) {
                                totalSelectedQuantity += ConverterUtils.toDouble(goodsPrice(cartGoodInfo)) * cartGoodInfo.getNum();
                            }
                        }
                    }
                }
            }
        }
        return totalSelectedQuantity;
    }

    /**
     * 计算选中的购物车数量价格
     *
     * @return
     */
    public double calculateTotalSelectedPrice() {

        double totalSelectedQuantity = 0;

        List<CartShopInfo> shoppingList = getShopInfoList();

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return totalSelectedQuantity;
        }

        for (CartShopInfo shoppingItem : shoppingList) {
            for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                if (isCheck(cartGoodInfo)) {
                    totalSelectedQuantity += ConverterUtils.toDouble(goodsPrice(cartGoodInfo)) * cartGoodInfo.getNum();
                }
            }
        }
        return totalSelectedQuantity;
    }


    /**
     * 计算选中不同类型的购物车数量
     *
     * @return
     */
    public int calculateTotalSelectedCartTypeNum(String cartType, String shopId, boolean isCartType) {
        int totalSelectedQuantity = 0;
        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return 0;
        }
        List<CartShopInfo> shoppingList = getShopInfoList();
        for (CartShopInfo shoppingItem : shoppingList) {
            if (cartType.equals(shoppingItem.getCartType())) {
                for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                    if (isCartType) {
                        if (isCheck(cartGoodInfo)) {
                            totalSelectedQuantity += cartGoodInfo.getNum();
                        }
                    } else {
                        if (isCheck(cartGoodInfo) && shopId.equals(cartGoodInfo.getShopId())) {
                            totalSelectedQuantity += cartGoodInfo.getNum();
                        }
                    }

                }
            }
        }
        return totalSelectedQuantity;
    }

    /**
     * 计算选中不同类型的购物车数量价格
     *
     * @return
     */
    public double calculateTotalSelectedCartTypePrice(String cartType, String shopId, boolean isCartType) {

        double totalSelectedQuantity = 0;

        List<CartShopInfo> shoppingList = getShopInfoList();

        if (Preconditions.isNullOrEmpty(getShopInfoList())) {
            return totalSelectedQuantity;
        }

        for (CartShopInfo shoppingItem : shoppingList) {
            if (cartType.equals(shoppingItem.getCartType())) {
                for (CartGoodInfo cartGoodInfo : shoppingItem.getCartInfos()) {
                    if (isCartType) {
                        if (isCheck(cartGoodInfo)) {
                            totalSelectedQuantity += ConverterUtils.toDouble(goodsPrice(cartGoodInfo)) * cartGoodInfo.getNum();
                        }
                    } else {
                        if (isCheck(cartGoodInfo) && shopId.equals(cartGoodInfo.getShopId())) {
                            totalSelectedQuantity += ConverterUtils.toDouble(goodsPrice(cartGoodInfo)) * cartGoodInfo.getNum();
                        }
                    }

                }
            }
        }
        return totalSelectedQuantity;
    }

    /**
     * 跳转确认订单回调
     */
    public void toConfirmOrderCallBack() {
        if (shopCartBalanceDialog != null) {
            shopCartBalanceDialog.setConfirmOrderListener(new ShopCartBalanceDialog.ConfirmOrderListener() {
                @Override
                public void toConfirmOrder(String cartType, int position) {
                    toConfirmOrderActivity(cartType, position);
                }
            });
        }
    }

    /**
     * 商品价格处理
     *
     * @param cartInfos
     * @return
     */
    private String goodsPrice(CartGoodInfo cartInfos) {
        if (!Preconditions.isNullOrEmpty(cartInfos.getDiscountPrice())) {
            return cartInfos.getDiscountPrice();
        } else {
            return cartInfos.getPrice();
        }
    }

    /**
     * 判断是否选中
     *
     * @param item
     * @return
     */
    private boolean isCheck(@NonNull CartGoodInfo item) {
        return shoppingCartStatus.isCheck(item);
    }

    /**
     * 删除有效购物车商品
     *
     * @param onLoadListener
     */
    @SuppressLint("CheckResult")
    public void deleteCartGoodsInfo(OnLoadListener<BaseResponseInfo> onLoadListener) {
        Observable<BaseResponseInfo> infoObservable = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShopCartApi.class)
                .getDeleteCartGoods(cartIds(0, false, 0), AppConstants.APPID));
        new RxLoadingWrapper<BaseResponseInfo>(mContext)
                .execute(infoObservable)
                .subscribe(new Consumer<BaseResponseInfo>() {
                    @Override
                    public void accept(BaseResponseInfo baseResponseInfo) throws Exception {
                        if (baseResponseInfo == null) {
                            return;
                        }
                        onLoadListener.onSuccess(baseResponseInfo);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }

    @SuppressLint("CheckResult")
    public void deleteCartInvalidGoodsInfo(long[] ids, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Observable<BaseResponseInfo> infoObservable = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShopCartApi.class)
                .getDeleteCartGoods(ids, AppConstants.APPID));
        new RxLoadingWrapper<BaseResponseInfo>(mContext)
                .execute(infoObservable)
                .subscribe(new Consumer<BaseResponseInfo>() {
                    @Override
                    public void accept(BaseResponseInfo baseResponseInfo) throws Exception {
                        if (baseResponseInfo == null) {
                            return;
                        }
                        onLoadListener.onSuccess(baseResponseInfo);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }
}
